package org.jianfa.learn.mongodb;

import org.jianfa.learn.mongodb.entity.Resume;
import org.jianfa.learn.mongodb.repository.ResumeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class PracticeMongodbShardingApplication {

    private static final Logger logger = LoggerFactory.getLogger(PracticeMongodbShardingApplication.class);

    @Autowired
    private ResumeRepository resumeRepository;

    public static void main(String[] args) {
        SpringApplication.run(PracticeMongodbShardingApplication.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner() {
        return new CommandLineRunner() {
            @Override
            public void run(String... args) throws Exception {
                for (int i=0; i<=1000; i++) {
                    Resume resume = new Resume();
                    String name = "test" + i;
                    double salary = Math.random()* 20000;
                    resume.setName(name);
                    resume.setSalary(salary);
                    Resume insertResume = resumeRepository.insert(resume);
                    String id = insertResume.getId();
                    logger.info("插入第{}个数据 id:{} name:{} salary:{}", i, id, name, salary);
                }
            }
        };
    }

}
